package com.sright.bcard.cms.controller.enterprise.apply;

import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.excel.ExcelExport;
import com.sright.bcard.common.service.share.excel.style.DefaultExcelStyle;
import com.sright.bcard.common.service.share.excel.style.ExcelStyle;
import com.sright.bcard.common.service.share.excel.utils.ExcelType;
import com.sright.bcard.common.service.share.mvcresolver.BodyFiled;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.common.service.share.dto.content.MessageDTO;
import com.sright.bcard.content.share.service.MessageService;
import com.sright.bcard.common.service.share.dto.enterprise.EnterpriseDTO;
import com.sright.bcard.enterprise.share.request.EnterpriseInfoParam;
import com.sright.bcard.enterprise.share.service.EnterpriseService;
import com.sright.common.response.SrightResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: lumf
 * Date: 2020/8/18 9:55 AM
 */
@RestController
@Slf4j
public class EApplyManageController {
    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MessageService messageService;

    @PostMapping("/apply/open")
    public SrightResponse apply(@RequestBody EnterpriseInfoParam param) throws ParseException {
        EnterpriseDTO enterpriseDTO = new EnterpriseDTO();
        DataConvertUtils.convertDO2DTO(param, enterpriseDTO);
        Boolean success = enterpriseService.confirmApply(enterpriseDTO);
        return SrightResponse.defaultSuccess(success);
    }

    @EnableLoginAuth
    @GetMapping("/apply/detail")
    public SrightResponse getApplyDetailInfo(@RequestParam("id") Long applyId) {
        MessageDTO messageDTO = messageService.getDetailById(applyId);
        return SrightResponse.defaultSuccess(messageDTO);
    }

    @EnableLoginAuth
    @GetMapping("/apply/list")
    public SrightResponse getApplyList(@RequestParam(value = "keyword", required = false) String keyword,
                                       @RequestParam(value = "status",required = false) Integer status,
                                       @RequestParam(value = "is_opening", required = false) Integer isOpened,
                                       @RequestParam("page") Integer pageIndex,
                                       @RequestParam("pageSize") Integer pageSize

    ) {
        Pager<MessageDTO> pager = messageService.searchByKeyword(
                keyword, status, isOpened, pageIndex, pageSize
        );
        return SrightResponse.defaultSuccess(pager);
    }

    @PostMapping("/apply/update")
    @EnableLoginAuth
    public SrightResponse updateApplyInfo(@BodyFiled Long id, @BodyFiled Integer status,
                                          @BodyFiled String content
    ) {
        boolean success = messageService.updateById(id, status, content);
        return SrightResponse.defaultSuccess(success);
    }



    @GetMapping(value = "/apply/export", produces = "application/json;charset=UTF-8")
    @EnableLoginAuth
    @ResponseBody
    public void exportOrder(HttpServletResponse response, @RequestParam(value = "keyword", required = false) String keyword,
                            @RequestParam(value = "status", required = false) Integer status,
                            @RequestParam(value = "is_opening", required = false) Integer isOpened,
                            @RequestParam("page") Integer pageIndex,
                            @RequestParam("pageSize") Integer pageSize) {
        try {
            Pager<MessageDTO> pager = messageService.searchByKeyword(
                    keyword, status, isOpened, pageIndex, pageSize
            );
            if (CollectionUtils.isNotEmpty(pager.getList())) {
                OutputStream out = response.getOutputStream();
                response.setContentType("multipart/form-data;charset=utf-8");
                response.setCharacterEncoding("utf-8");
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
                String filename = "apply_" + LocalDate.now().format(fmt) + ".xlsx";
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
                ExcelExport excel = new ExcelExport(MessageExportInfo.class);
                List<MessageExportInfo> list = new ArrayList<>();
                for (MessageDTO dto : pager.getList()) {
                    list.add(MessageExportInfo.getInstance(dto));
                }
                ExcelStyle excelStyle = new DefaultExcelStyle(ExcelType.xlsx);
                excel.exportExcel(list, "试用申请", out, excelStyle);
            }
        } catch (IOException e) {
            log.info("exportOrder"+e.getMessage());
        }
    }

}
